package com.drool.engine.controller;

import com.drool.engine.entity.Student;
import org.drools.core.base.RuleNameStartsWithAgendaFilter;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.QueryResults;
import org.kie.api.runtime.rule.QueryResultsRow;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/rule")
public class RuleController {

    @RequestMapping("/param")
    public void param (){
        try {
            KieServices ks = KieServices.Factory.get();
            KieContainer kieContainer = ks.getKieClasspathContainer();
            Student student = new Student("张三",8);
            Student student2 = new Student("李四",9);
            Student student3 = new Student("王五",22);
            KieSession kieSession = kieContainer.newKieSession();
            kieSession.insert(student);
            kieSession.insert(student2);
            kieSession.insert(student3);
/**指定组获得焦点*/
            //kieSession.getAgenda().getAgendaGroup("");

/**激活所有规则*/
            //kieSession.fireAllRules();


            /**
             * 全局变量赋值 ,不赋值会报空指针
             */
            kieSession.setGlobal("count",0);
            List list = new ArrayList();
            kieSession.setGlobal("gList",list);

 //调用规则文件中的查询
            QueryResults results1 = kieSession.getQueryResults("query_1");
            int size = results1.size();
            System.out.println("size=" + size);
            for (QueryResultsRow row : results1) {
                student = (Student) row.get("$student");
                System.out.println(student);
            }
            QueryResults results2 = kieSession.getQueryResults("query_2","王五");
            size = results2.size();
            System.out.println("size=" + size);
            for (QueryResultsRow row : results2) {
                 student = (Student) row.get("$student");
                System.out.println(student);
            }
            kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("query"));
/**
 * 通过规则过滤器实现只执行指定规则
 * RuleNameEndsWithAgendaFilter
 * RuleNameEqualsAgendaFilter
 * RuleNameMatchesAgendaFilter
 * RuleNameSerializationAgendaFilter
 * RuleNameStartsWithAgendaFilter
 */

           /* kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("classFor"));
            System.out.println("******************************");
            System.out.println(student);
            System.out.println(student2);
            System.out.println(student3);
            System.out.println("******************************");*/


            //kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("rule_date"));

 /**使用timer属性*/
          /*  new Thread(new Runnable() {
                @Override
                public void run() {
                    //启动规则引擎进行规则匹配，直到调用halt方法才结束规则引擎
                    kieSession.fireUntilHalt(new RuleNameStartsWithAgendaFilter("rule_timer"));
                }
            }).start();
            Thread.sleep(10000);
            //结束规则引擎
            kieSession.halt();
*/

/**关闭session*/
            kieSession.dispose();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
